package com.meiqiongproperty.yiguanjia.fragment.home.health;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.OpenMemberLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.pay.PayTypeFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendChargeProperty;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayWX;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayZFB;
import com.meiqiongproperty.yiguanjia.ui.dialog.FragmentExchangeController;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * author: Administrator
 * date: 2017/5/21.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.health
 */

public class OpenMemberFragment extends BaseFragment {
    OpenMemberLayoutBinding binding;
    int type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.open_member_layout,container,false);
        binding.setClick(this);
        binding.navigation.setTitle("开通会员");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        return binding.getRoot();
    }


    /**
     * 支付
     */
    public void select_pay_type(View view){
        PayTypeFragment payTypeSeleor = new PayTypeFragment();
        payTypeSeleor.setPayTypeClickListener(new PayTypeFragment.PayTypeClickListener() {
            @Override
            public void setData(Object data) {
                type= (int) data;
                switch (type){
                    case 1:
                        binding.type.setText("支付宝支付");
                        break;
                    case 2:
                        binding.type.setText("微信支付");
                        break;
                }
            }
        });
        FragmentExchangeController.addFragment(getFragmentManager(), payTypeSeleor, payTypeSeleor.getTagName());
    }


    public void openMemeberPayClick(View view){
        if (Utils.isFastDoubleClick()){
            return;
        }
        SendChargePropertyMethod();
    }



    private void SendChargePropertyMethod() {
        SendChargeProperty sendChargeProperty=new SendChargeProperty();
        sendChargeProperty.setPayMethod(type);
        sendChargeProperty.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        sendChargeProperty.setRoomID(DBUserUtils.getmDefaultHouse(getActivity()).getID());
        switch (type){
            case 1:
                ApiService.chargeProperty(getActivity(), new SimpleHttpCallback<CommPayZFB>() {
                    @Override
                    public void onNext(CommPayZFB commPayZFB) {
                        String orderInfo = commPayZFB.getParameter().getParameter();   // 订单信息
                        doAlipay(orderInfo);
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
            case 2:
                ApiService.chargePropertyWX(getActivity(), new SimpleHttpCallback<CommPayWX>() {
                    @Override
                    public void onNext(CommPayWX commPayWX) {
                        doWXPay(commPayWX.getParameter().getParameter(),commPayWX.getParameter().getParameter().getAppid());
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
        }

    }
}
